package com.vgemv.jsutilitysdkdemo.module.List.ExtendContent;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuItemView.JsMenuItemView;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuItemVm.JsMenuItemVm;
import com.vgemv.jsutilitysdkdemo.R;

public class ExtendContentItemView extends JsMenuItemView {


    private RelativeLayout mrl_root;
    private TextView mtv_context;
    private ImageView miv_header ;
    private ImageView miv_logo;
    private TextView mtv_time;

    public ExtendContentItemView(Context context) {
        super(context);
    }


    @Override
    public void customSetVm(Context context, JsMenuItemVm menuItemVm) {
        ExtentContentItemViewVm vm = (ExtentContentItemViewVm) menuItemVm;
        {
            if(mrl_root == null) {
            mrl_root = new RelativeLayout(context);
            mrl_root.setId(View.generateViewId());
            mrl_root.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.MATCH_PARENT));
            addView(mrl_root);
            }
        }

        {
            //title
            if(mtv_context == null) {
                mtv_context = new TextView(context);
                mtv_context.setId(View.generateViewId());
                mrl_root.addView(mtv_context);
            }

                RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ExtentContentItemViewVm.contextWidth, ExtentContentItemViewVm.contextHeight);
                lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
                lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                lp.setMargins(ExtentContentItemViewVm.contentLeftPadding, ExtentContentItemViewVm.contentTopPadding,0, 0);
                mtv_context.setLayoutParams(lp);


            mtv_context.setText(vm.model.title);
            mtv_context.setTextAlignment(TEXT_ALIGNMENT_TEXT_START);
            mtv_context.setGravity(Gravity.TOP);
            mtv_context.setTextSize(15);
            mtv_context.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
            mtv_context.setTextColor(Color.argb(255,0,0,0));
            mtv_context.setBackgroundColor(Color.argb(255,255,255,255));

        }
        {
            //logo
            if(miv_logo == null) {
                miv_logo = new ImageView(context);
                miv_logo.setId(View.generateViewId());
                mrl_root.addView(miv_logo);
            }
            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ExtentContentItemViewVm.logoSize, ExtentContentItemViewVm.logoSize);
            lp.addRule(RelativeLayout.BELOW,mtv_context.getId());
            lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            lp.setMargins(ExtentContentItemViewVm.contentLeftPadding, ExtentContentItemViewVm.contentLogoSpacing,0, ExtentContentItemViewVm.logoBottomPadding);
            miv_logo.setLayoutParams(lp);
            miv_logo.setImageResource(R.mipmap.logo_news);
        }
        {
            //time
            if(mtv_time == null) {
                mtv_time = new TextView(context);
                mtv_time.setId(View.generateViewId());
                mrl_root.addView(mtv_time);
            }
            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ExtentContentItemViewVm.logoSize);
            lp.addRule(RelativeLayout.BELOW,mtv_context.getId());
            lp.addRule(RelativeLayout.RIGHT_OF,miv_logo.getId());
            lp.setMargins(ExtentContentItemViewVm.logoTimeSpacing, ExtentContentItemViewVm.contentLogoSpacing,0, 0);
            mtv_time.setLayoutParams(lp);

            String content = String.format("%s·%s","独特客户端",vm.model.publishDatetime.substring(14,19));
            mtv_time.setText(content);
            mtv_time.setGravity(Gravity.CENTER);
            mtv_time.setTextColor(Color.argb(255,180,180,180));
            mtv_time.setBackgroundColor(Color.argb(255,255,255,255));

        }

        {
            //header
            if(miv_header == null) {
                miv_header = new ImageView(context);
                miv_header.setId(View.generateViewId());
                mrl_root.addView(miv_header);
            }
            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ExtentContentItemViewVm.imageViewWidth, ExtentContentItemViewVm.imageViewHeight);
            lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
            lp.addRule(RelativeLayout.RIGHT_OF,mtv_context.getId());
            lp.setMargins(ExtentContentItemViewVm.contentImageViewSpacing, ExtentContentItemViewVm.contentTopPadding,0, 0);
            miv_header.setLayoutParams(lp);

            String imageUrlString = vm.model.imagePath;
            Picasso.get().load(imageUrlString)
                    .placeholder(com.vgemv.jsutilitysdk.R.mipmap.placeholder)
                    .error(com.vgemv.jsutilitysdk.R.mipmap.placeholder)
                    .into(miv_header);
        }
    }

}
